/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.ticket;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.ticket.Ticket;
import com.boxin.ims.modules.wechat.service.ticket.TicketService;

/**
 * 入场券Controller
 * @author Jakemanse
 * @version 2014-01-28
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/ticket/ticket")
public class TicketController extends BaseController {

	@Autowired
	private TicketService ticketService;
	
	@ModelAttribute
	public Ticket get(@RequestParam(required=false) Long id) {
		if (id != null){
			return ticketService.get(id);
		}else{
			return new Ticket();
		}
	}
	
	@RequiresPermissions("wechat:ticket:ticket:view")
	@RequestMapping(value = {"list", ""})
	public String list(Ticket ticket, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			ticket.setUser(user);
		}
        Page<Ticket> page = ticketService.find(new Page<Ticket>(request, response), ticket); 
        model.addAttribute("page", page);
		return "modules/wechat/ticket/ticketList";
	}

	@RequiresPermissions("wechat:ticket:ticket:view")
	@RequestMapping(value = "form")
	public String form(Ticket ticket, Model model) {
	
		User user = UserUtils.getUser();
		if (ticket.getUser() == null || ticket.getUser().getId() <1){
			ticket.setUser(user);
		}
		
		model.addAttribute("ticket", ticket);
		return "modules/wechat/ticket/ticketForm";
	}

	@RequiresPermissions("wechat:ticket:ticket:edit")
	@RequestMapping(value = "save")
	public String save(Ticket ticket, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ticket)){
			return form(ticket, model);
		}
		ticketService.save(ticket);
		addMessage(redirectAttributes, "保存入场券'" + ticket.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/ticket/ticket/?repage";
	}
	
	@RequiresPermissions("wechat:ticket:ticket:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		ticketService.delete(id);
		addMessage(redirectAttributes, "删除入场券成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/ticket/ticket/?repage";
	}

}
